﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using System.Data;
using System.IO;
using System.Configuration;
using System.Transactions;
using System.Data.Linq;
using System.ComponentModel;
using UtilityExtensions;

namespace BOL.Ventas
{
    public class GetFinanciamientoList
    {
        int _count;





        public int Count(int idOrdenVenta,
                         int startRowIndex,
                         int maximumRows,
                         string sortExpression)
        {
            return _count;
        }



        private IQueryable<FinanciamientoListInfo> ApplySelect_ListInfo()
        {
            var q = from a in DbUtil.Db.Financiamientos
                    select new FinanciamientoListInfo
                    {
                        comentario=a.comentario,
                        fecha=a.fecha,
                        fechaStr=a.fecha.ToShortDateString(),
                        idInternal=a.idInternal,
                        idOrdenVenta=a.idOrdenVenta,
                        ordenVentaStr=a.OrdenVenta.numDocumento,
                        numDocumento=a.numDocumento,
                        Origen=a.FinanciamientoItems,
                        destino=a.OrdenVentaFormatos,
                        totalCuota=a.total,
                        totalCuotaStr=string.Format("{0:N}",a.total)
                    };
            return q;
        }
        private IQueryable<FinanciamientoListInfo> ApplySort(IQueryable<FinanciamientoListInfo> q, string sortExpression)
        {
            switch (sortExpression)
            {
                case "comentario":
                    q = q.OrderBy(b => b.comentario);
                    break;
                case "comentario DESC":
                    q = q.OrderByDescending(b => b.comentario);
                    break;

                case "numDocumento":
                    q = q.OrderBy(b => b.numDocumento);
                    break;
                case "numDocumento DESC":
                    q = q.OrderByDescending(b => b.numDocumento);
                    break;

               

                case "fecha":
                    q = q.OrderBy(b => b.fecha);
                    break;
                case "fecha DESC":
                    q = q.OrderByDescending(b => b.fecha);
                    break;


               

                case "ordenVentaStr":
                    q = q.OrderBy(b => b.ordenVentaStr);
                    break;
                case "ordenVentaStr DESC":
                    q = q.OrderByDescending(b => b.ordenVentaStr);
                    break;


                case "totalCuota":
                    q = q.OrderBy(b => b.totalCuota);
                    break;
                case "totalCuota DESC":
                    q = q.OrderByDescending(b => b.totalCuota);
                    break;

                
                default:
                    q = q.OrderByDescending(b => b.fecha).OrderByDescending(p => p.numDocumento);
                    break;
            }
            return q;
        }


        [DataObjectMethod(DataObjectMethodType.Select, false)]
        public IEnumerable<FinanciamientoListInfo> Execute(int idOrdenVenta,
                                                             int startRowIndex,
                                                             int maximumRows,
                                                             string sortExpression)
        {


            var q = ApplySelect_ListInfo();


            q = q.Where(a => a.idOrdenVenta == idOrdenVenta );


            _count = q.Count();

            q = ApplySort(q, sortExpression);
            return q.Skip(startRowIndex).Take(maximumRows);
        }



                        

    }
}
